<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实战 - 调用Mock生成的莫兰迪色系图片 - 纯js</title>
</head>

<body>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <h2>F12打开控制台查看数据，效果更佳</h2>
    借鉴：https://blog.csdn.net/weixin_44500183/article/details/111413173
    <div id="Application">
        <h1>调用Mock生成的莫兰迪色系图片</h1>
        <ul>
            <div id="content"></div>
        </ul>
    </div>



    <script>

        let Random = Mock.Random;


        //生成数据
        let arr = Mock.mock(() => {
            const data = []
            for (let i = 0; i < 16; i++) {
                // 利用mockjs的Random随机生成数字并转成十六进制，拼接。
                const a = '#' + Random.integer(180, 255).toString(16) +
                    Random.integer(140, 255).toString(16) +
                    Random.integer(120, 220).toString(16)
                data.push({
                    image: Random.image('140x140', a, ' IMAGE '),
                    productName: Random.ctitle(),//产品名
                    productPrice: Random.float(10, 1000, 2, 3),//价格
                    productSell: Random.integer(100, 10000),//数量
                    isFreeShipping: Random.boolean()//是否包邮
                })
            }
            return data
        })



        //展示数据
        console.table(arr);
        x = document.getElementById("content");
        //将对象转为JSON
        //x.innerHTML = JSON.stringify(imgData, null, '<br />');

        //循环打印
        for (let i = 0; i < arr.length; i++) {

            let image = arr[i]['image'];//当前图片
            let productName = arr[i]['productName'];//产品名
            let productPrice = arr[i]['productPrice'];//价格
            let productSell = arr[i]['productSell'];//数量
            let isFreeShipping = arr[i]['isFreeShipping'];//是否包邮

            if (isFreeShipping) {
                isFreeShipping = "是"
            } else {
                isFreeShipping = "否"
            }

            //组合
            x.innerHTML += "<li><img src=" + image + " /></li><li>产品名：" + productName + "</li> <li>价格：" + productPrice + "元</li><li>数量：" + productSell + "件</li><li>是否包邮：" + isFreeShipping + "</li>";
        }

    </script>

</body>

</html>